package com.example.makerspace.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.makerspace.domain.DemandDO;
import com.example.makerspace.domain.EnterpriseCertificationDO;
import com.example.makerspace.domain.UserDO;
import com.example.makerspace.dto.PageRequest;
import com.example.makerspace.dto.Pagination;
import com.example.makerspace.mapper.EnterpriseCertificationMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import  java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;


@Component
public class EnterpriseCertificationDAO  extends  BaseDAO<EnterpriseCertificationDO>{
    @Resource
    private EnterpriseCertificationMapper enterpriseCertificationMapper;

    @PostConstruct
    public void init(){
        this.mapper = enterpriseCertificationMapper;
    }

    public List<EnterpriseCertificationDO> selectByPhoneNumber(String username) {
        return  mapper.selectList(new QueryWrapper<EnterpriseCertificationDO>().eq("phone_number",username));
    }

    public void deleteByUsername(String username) {
        mapper.delete(new QueryWrapper<EnterpriseCertificationDO>().eq("phone_number",username));
    }

    public void updateByPhoneNumber(EnterpriseCertificationDO enterpriseCertificationDO) {
        UpdateWrapper<EnterpriseCertificationDO>  updateWrapper= new UpdateWrapper<>();
        updateWrapper.eq("phone_number",enterpriseCertificationDO.getPhoneNumber());
        mapper.update(enterpriseCertificationDO,updateWrapper);
    }

    public IPage<EnterpriseCertificationDO> selectByPageByEntName(Pagination pagination, String entName) {
        QueryWrapper<EnterpriseCertificationDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.like("ent_name",entName).or().like("phone_number",entName);
        return mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),queryWrapper);
    }

    public List<EnterpriseCertificationDO> selectByEntName(String entName) {
        QueryWrapper<EnterpriseCertificationDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.like("ent_name",entName);
        return mapper.selectList(queryWrapper);
    }

    public List<EnterpriseCertificationDO> selectHave() {
        QueryWrapper<EnterpriseCertificationDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("certification_results",1);
        return  mapper.selectList(queryWrapper);
    }

    public IPage<EnterpriseCertificationDO> selectByPageByEntNameAndState(Pagination pagination, String name, String state) {
        Integer certificationResults = null;
        if(!state.equals("")){
            certificationResults = Integer.parseInt(state);
        }
        LambdaQueryWrapper<EnterpriseCertificationDO> qw = new LambdaQueryWrapper<EnterpriseCertificationDO>();
        qw.eq(StringUtils.isNotEmpty(state), EnterpriseCertificationDO::getCertificationResults, certificationResults).and(wrapper ->
                wrapper.like(StringUtils.isNotEmpty(name),EnterpriseCertificationDO::getEntName,name).or()
                        .like(StringUtils.isNotEmpty(name),EnterpriseCertificationDO::getPhoneNumber,name)).orderByDesc(EnterpriseCertificationDO::getCreateTime);
        return  mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),qw);

    }

    public IPage<EnterpriseCertificationDO> selectByState(Pagination pagination, String state) {
        QueryWrapper<EnterpriseCertificationDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("certification_results",state);
        return mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),queryWrapper);
    }

    public IPage<EnterpriseCertificationDO> selectByPage(Pagination pagination) {
        QueryWrapper<EnterpriseCertificationDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        return mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),queryWrapper);
    }
}
